using System;
using System.Collections.Generic;
using System.Text;

namespace masterCard
{
    class BridgeDisplay
    {

        private readonly System.Windows.Forms.Form frmHost;
        private int nCardHeight = 110;
        private int nCardWidth = 80;
        private int nMargin = 20;
        private int nVerticalCardDistance = 20;
        private int nHorizantalCardDistance = 20;
        private int nHeight = 0;
        private int nWidth = 0;
        public BridgeDisplay(System.Windows.Forms.Form host)
        {
            frmHost = host;
            nHeight = frmHost.Height;
            nWidth = frmHost.Width;
        }
        internal void Draw(BridgePlayer player)
        {

            switch (player.Position)
            {
                case Position.North:
                case Position.South:
                    DrawNorthSouth(player);
                    break;
                case Position.East:
                case Position.West:
                    DrawEastWest(player);
                    break;
            }
        }

        private void DrawEastWest(BridgePlayer player)
        {
            int dx = -nHorizantalCardDistance;
            int dy = nVerticalCardDistance + nCardHeight;

            int X0 = nMargin;
            int Y0 = nHeight / 2 - 2 * dy; ;

            Card[] hand = player.GetHand();
            if (player.Position == Position.West)
            {
                X0 = nMargin + (hand.Length - 1) * nVerticalCardDistance;
            }
            else if (player.Position == Position.East)
            {
                X0 = nWidth - nMargin - nCardWidth;
            }
            int X = X0;
            int Y = Y0;

            foreach (CardSuit cs in CardSuit.GetValues(typeof(CardSuit)))
            {
                X = X0;
                for (int handIndex = 0; handIndex < hand.Length; handIndex++)
                {
                    Card card = hand[handIndex];
                    if (card.GetCardSuit().ToString() == cs.ToString())
                    {
                        ShowCard(card, X, Y);
                        X += dx;
                    }
                }
                Y += dy;
            }
        }

        private void DrawNorthSouth(BridgePlayer player)
        {
            int dx = nCardWidth;
            int dy = -nVerticalCardDistance;

            int X0 = nWidth / 2 - dx;
            int Y0 = nMargin;

            Card[] hand = player.GetHand();
            if (player.Position == Position.South)
            {
                Y0 = nHeight - nMargin - nCardHeight;
            }
            else if (player.Position == Position.North)
            {
                Y0 = nMargin + (hand.Length - 1) * nVerticalCardDistance;
            }
            int Y = Y0;
            int X = X0;
            foreach (CardSuit cs in CardSuit.GetValues(typeof(CardSuit)))
            {
                Y = Y0;
                for (int handIndex = 0; handIndex < hand.Length; handIndex++)
                {
                    Card card = hand[handIndex];
                    if (card.GetCardSuit().ToString() == cs.ToString())
                    {
                        ShowCard(card, X, Y);
                        Y += dy;
                    }
                }
                X += dx;
            }
        }
        private void ShowCard(Card card, int X, int Y)
        {
            System.Windows.Forms.PictureBox aPic = new System.Windows.Forms.PictureBox();
            aPic.Click += new System.EventHandler(ClickHandler);
            aPic.MouseHover += new System.EventHandler(MouseHoverHandler);
            aPic.Left = X;
            aPic.Top = Y;
            aPic.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
            aPic.Image = Resources.GetImage(card.GetImageName());
            frmHost.Controls.Add(aPic);
            aPic.Tag = card.GetSymbolText();
            aPic.Text = card.GetSymbolText();
        }
        public void ClickHandler(Object sender, System.EventArgs e)
        {
            object tag = ((System.Windows.Forms.PictureBox)sender).Tag;
            System.Windows.Forms.MessageBox.Show("You have clicked button " + tag.ToString());
            Card card = new Card((string)tag);
            //frmHost.Controls.Remove(((System.Windows.Forms.PictureBox)sender));
        }
        public void MouseHoverHandler(Object sender, System.EventArgs e)
        {
            //object tag = ((System.Windows.Forms.PictureBox)sender).Tag;
            //((System.Windows.Forms.PictureBox)sender).System.Windows.Forms.MessageBox.Show("You have clicked button " + tag.ToString());
            //frmHost.Controls.Remove(((System.Windows.Forms.PictureBox)sender));
        }
        
        internal void DrawState(GameState m_eGameState)
        {
            // frmHost.
            //frmBridgeMasterCard.get(PlayButton);
        }
    }

}
